package com.ravi.core;

import com.ravi.dao.CheckRoomDAO;
import com.ravi.entity.CheckRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class GlobalRoomManager {

	public static final ConcurrentHashMap<Integer, RoomChecking> roomManager = new ConcurrentHashMap<>();

	@Autowired
	private CheckRoomDAO checkRoomDAO;

	public static void addRoom(RoomChecking roomChecking) {
		roomManager.put(roomChecking.getCheckRoom().getRoomId(), roomChecking);
	}

	@PostConstruct
	public void init() {
		List<CheckRoom> checkRooms = checkRoomDAO.getAllCheckRooms();
		RoomChecking roomChecking = null;
		for (CheckRoom room : checkRooms) {
			roomChecking = new RoomChecking(room);
			addRoom(roomChecking);
		}
	}

	@PreDestroy
	public void destroy() {
		//系统运行结束
	}

	public static RoomChecking getRoomCheckingById(Integer roomId) {
		return roomManager.get(roomId);
	}

}
